package com.ls.lserp.modules.material.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ls.lserp.modules.sys.utils.SystemMessageUtils;
import org.apache.commons.collections.map.HashedMap;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ls.lserp.common.config.Global;
import com.ls.lserp.common.persistence.AjaxDone;
import com.ls.lserp.common.persistence.BaseEntity;
import com.ls.lserp.common.persistence.Page;
import com.ls.lserp.common.utils.ResourcesUtil;
import com.ls.lserp.common.web.BaseController;
import com.ls.lserp.modules.material.entity.InventoryRequestEx;
import com.ls.lserp.modules.material.entity.MaterialOutInbound;
import com.ls.lserp.modules.material.service.MaterialOutboundService;
import com.ls.lserp.modules.order.entity.StyleOptionEntity;
import com.ls.lserp.modules.sys.entity.Dict;
import com.ls.lserp.modules.sys.utils.DictUtils;
import freemarker.template.Configuration;
import groovy.util.ResourceException;


@Controller
@RequestMapping(value = "${adminPath}/material/outbound/")
public class MaterialOutboundController extends BaseController {
	// 保存 编辑 删除 方法
	@Autowired
	private MaterialOutboundService materialOutboundService;
	@Autowired
	Configuration freemarkerConfiguration;
	/**
	 * 一览画面
	 * @param materialOutInbound
	 * @param request
	 * @param response
	 * @return
	 */
	@RequiresPermissions("material:outbound:view")
	@RequestMapping(value = { "searchList" })
	@ResponseBody
	public String searchList(MaterialOutInbound materialOutInbound, HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<Object, Object> listMap = new HashMap<Object, Object>();
		// ajax回调时使用的一览list名
		request.setAttribute(Global.LIST_NAME, "materialOutbound");
		// 画面取得的检索项目
		// 拼接字符串供分页使用
				StringBuilder searchParam = new StringBuilder();
		// 出库单号
				searchParam.append("&inventoryRequestNo=");
				searchParam.append(materialOutInbound.getInventoryRequestNo());
				// 加工合同号
				searchParam.append("&contractProcessNo=");
				searchParam.append(materialOutInbound.getContractProcessNo());
				// 加工厂名称
				searchParam.append("&fabricatingName=");
				searchParam.append(materialOutInbound.getFabricatingName());
				// 日期
				searchParam.append("&dateFrom=");
				searchParam.append(materialOutInbound.getDateFrom());
				searchParam.append("&dateTo=");
				searchParam.append(materialOutInbound.getDateTo());
				// 状态
				searchParam.append("&tabPageStatus=");
				searchParam.append(materialOutInbound.getTabPageStatus());
		
		// 放到Attribute里，使用Global类里面的SEARCH_PAEAM作为key
		request.setAttribute(Global.SEARCH_PAEAM, searchParam.toString());
		// 查询一览页面数据
		Page<MaterialOutInbound> page = materialOutboundService.findList(new Page<MaterialOutInbound>(request, response),
				materialOutInbound);
		// 读取系统路径
		// 读取文件生成字符串
		String strUri = request.getContextPath() + Global.getAdminPath();
		String strHtml = "";
		Map<String, Object> map = new HashedMap();
		map.put("adminUri", strUri);
		map.put("materialOutbounds", page.getList());
		map.put("pageNo", page.getPageNo());
		map.put("pageSize", page.getPageSize());
		strHtml = FreeMarkerTemplateUtils
				.processTemplateIntoString(freemarkerConfiguration.getTemplate("materialOutbound.ftl"), map);
		listMap.put("materialOutbound", strHtml);
		listMap.put("page", page.getHtml());
		// ajax返回结果
		return renderString(response, listMap);
	}
	/**
	 * 进入一览页面
	 * 
	 * @return
	 */
	@RequiresPermissions("material:outbound:view")
	@RequestMapping(value = {""})
	public String list() {
		// 返回页面地址
	    return "modules/material/materialOutbound";
	}
	/**
	 * 创建物料出库信息窗口
	 * @param model
	 * @return
	 */
	@RequiresPermissions("material:outbound:view")
	@RequestMapping(value = {"show"})
	public String viewOutbound(MaterialOutInbound materialOutInbound,Model model){
		String inventoryCategory = materialOutInbound.getInventoryCategory();
		if ((ResourcesUtil.getConfig("outbound_category_produce")).equals(inventoryCategory)) {
			materialOutInbound.setInventoryTypeKbn(0L);
		}else if((ResourcesUtil.getConfig("outbound_category_putting")).equals(inventoryCategory)){
			materialOutInbound.setInventoryTypeKbn(1L);
		}
		if (materialOutInbound.getInventoryRequestNo().indexOf("OMTM.") != -1) {
			   List<StyleOptionEntity> contractList = materialOutboundService.findContractInfo();
			   model.addAttribute("contractList",contractList);
			   model.addAttribute("imageStr","customizationUnitCost");
		   }
		materialOutInbound = materialOutboundService.getInfo(materialOutInbound);
		model.addAttribute("materialOutInbound", materialOutInbound);
		return "modules/material/materialOutboundShow";
	}
	/**
	 * 创建物料出库信息窗口
	 * @param model
	 * @return
	 */
	@RequiresPermissions("material:outbound:view")
	@RequestMapping(value = {"materialDetailList"})
	public String materialDetailList(MaterialOutInbound materialOutInbound,Model model){
		List<MaterialOutInbound> detailList = materialOutboundService.findInventoryMaterialDetailList(materialOutInbound);
		materialOutInbound.setRequestDetailsList(detailList);
		model.addAttribute("materialOutInbound", materialOutInbound);
		return "modules/material/materialOutDetail";
	}
	/**
	    * 保存信息
	    * @param materialOutInbound
	    * @param response
	    * @return
	    * @throws ResourceException 
	    */
	   @RequiresPermissions("material:outbound:edit")
	   @RequestMapping(value = "save")
	   @ResponseBody
	   public String save(MaterialOutInbound materialOutInbound, HttpServletResponse response) throws ResourceException {

		   
	   	   // 保存入库申请
			materialOutboundService.saveMaterialDetail(materialOutInbound);
	   	
	   	   // 保存成功返回提示信息
	       return renderString(response,AjaxDone.ok(SystemMessageUtils.getMessageName( "warehouse_edit_save_msg_success")));
	   }
	   /**
	    * 保存数量
	    * @param materialOutInbound
	    * @param request
	    * @param model
	    * @param response
	    * @return
	    * @throws ResourceException 
	    */
	   @RequiresPermissions("material:outbound:edit")
	   @RequestMapping(value = "saveQuantity")
	   @ResponseBody
	   public String saveQuantity(MaterialOutInbound materialOutInbound, HttpServletRequest request, Model model, HttpServletResponse response , HttpSession session) throws ResourceException {
		   
	   	   // 保存入库申请
		   materialOutboundService.saveQuantity(materialOutInbound);
	   	
	   	   // 保存成功返回提示信息
	       return renderString(response,AjaxDone.ok(SystemMessageUtils.getMessageName( "warehouse_edit_save_msg_success")));
	   }
	   /**
		 * 创建物料入库信息窗口
		 * 
		 * @return
		 */
		@RequiresPermissions("material:outbound:edit")
		@RequestMapping(value = {"changeStatus"})
		public String changeStatus(MaterialOutInbound materialOutInbound,Model model){
			materialOutboundService.changeStatus(materialOutInbound);
			return "modules/material/materialOutboundShow";
		}
}
